/*
 * Copyright (c) 2012 copyright Gábor Heizer
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy,
 * modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software
 * is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

package hu.my.bnf;

import hu.my.bnf.validation.StringValidation;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
 * Encapsulate a general BNF error or warning.
 * <p/>
 * <p>This class can contain basic error or warning information from BNF packages</p>.
 */
public class BNFException extends Exception {

//--------------------------------------------------------------------------------------------------------------------------------
// Fields
//--------------------------------------------------------------------------------------------------------------------------------

    /**
     * Determines if a de-serialized file is compatible with this class.
     */
    private static final long serialVersionUID = -4500476137829090299L;

    /**
     * @serial The embedded exception if tunnelling, or null.
     */
    private final Exception exception;

//--------------------------------------------------------------------------------------------------------------------------------
// Constructors
//--------------------------------------------------------------------------------------------------------------------------------

    /**
     * Create a new BNFException.
     *
     * @param message The error or warning message.
     */
    public BNFException(@NotNull final String message) {
        this(message, null);
    }

    /**
     * Create a new BNFException from an existing exception.
     * <p/>
     * <p>The existing exception will be embedded in the new
     * one, but the new exception will have its own message.</p>
     *
     * @param message The detail message.
     * @param e       The exception to be wrapped in a BNFException.
     */
    public BNFException(@NotNull final String message, @Nullable final Exception e) {
        super(message);
        this.exception = e;
    }

//--------------------------------------------------------------------------------------------------------------------------------
// Methods
//--------------------------------------------------------------------------------------------------------------------------------

    /**
     * Return a detail message for this exception.
     *
     * @return The error or warning message.
     */
    @NotNull
    public final String getMessage() {
        return StringValidation.getValidString(super.getMessage());
    }

    /**
     * Override toString to pick up any embedded exception.
     *
     * @return A string representation of this exception.
     */
    @NotNull
    public final String toString() {
        return exception != null ? exception.toString() : super.toString();
    }

}
